package com.webside.points.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.PostEntity;
import com.webside.points.model.PostTypeEntity;
import com.webside.points.model.UserEntity;
import com.webside.points.service.PostService;
import com.webside.points.service.PostTypeService;
import com.webside.points.service.UserService;
import com.webside.points.wechat.PageUtil;

@Controller
@Scope("prototype")
@RequestMapping("/post/")
public class PostController {
	
	@Autowired
	PostService postService;
	
	@Autowired
	PostTypeService postTypeService;
	
	@Autowired
	UserService userService;

	@RequestMapping("findAllUI")
	public String findAllUI(Model model){
		
		System.out.println("进入文章查询：>>>");
		PageUtil pageUtil=new PageUtil();
		PostEntity postEntity=new PostEntity();
		int totalCount=postService.findCount(postEntity);
		pageUtil.setTotalCount(totalCount);
		pageUtil.setCurPage(1);
		model.addAttribute("pageUtil", pageUtil);
		
		return "serve/post";
	}
	
	@RequestMapping("findAll")
	public void findAll(Model model,PostEntity postEntity,HttpServletResponse response){
		System.out.println("进入查询文章：>>>"+postEntity);
		 PageUtil pageUtil=new PageUtil();
		 
		 if(String.valueOf(postEntity.getCurPage())!=null){
			 int cPage=Integer.valueOf(postEntity.getCurPage());
			 cPage=(cPage-1)*10;
			 postEntity.setCurPage(Integer.valueOf(cPage));
		 }
		 postEntity.setTitle("%"+postEntity.getTitle()+"%");
		 
		 List<PostEntity> list=postService.findAll(postEntity);
		  pageUtil.setContent(list);
		  model.addAttribute("pageUtil", pageUtil);
		  
		  String json = JSON.toJSONString(pageUtil);
			try {
				response.getWriter().write(json);
				response.flushBuffer();
			} catch (Exception e) {
				System.out.println("JsonError："+e);
			}
	}
	
	@RequestMapping("insert")
	public String insert(PostEntity postEntity){
		
		postEntity.setStatus("1");
		postEntity.setCreateTime(new Date(System.currentTimeMillis()));
		System.out.println("进入文章增加：>>>"+postEntity);
		
		System.out.println(postService.insert(postEntity));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("delete")
	public String delete(int id){
		
		System.out.println(postService.delete(id));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("update")
	public String update(PostEntity postEntity){
		
		postEntity.setCreateTime(new Date(System.currentTimeMillis()));
		
		System.out.println(postService.update(postEntity));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("findById")
	public String findById(int id,Model model){
		
		model.addAttribute("post",postService.findById(id));
		return "serve/postForm";
	}
	
	@RequestMapping("updateStatus")
	public String updateStatus(PostEntity postEntity){
		System.out.println(postService.updateStatus(postEntity));
		return "redirect:findAllUI";
	}
	
	/*@RequestMapping("findAllPost")
	public String findAllPost(String title,Model model){
		
		if(title!=null){
			try {
				title=URLDecoder.decode(title,"utf-8");
				title="%"+title+"%";
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("进入文章查询：>>>"+title);
		model.addAttribute("postList", postService.findAllPost(title));
		
		return "service/post";
	}*/
	
	@RequestMapping("findByPostTypeId")
	public String findByPostTypeId(int typeId,Model model,HttpServletRequest request){
	
		PostTypeEntity postTypeEntity = postTypeService.findById(typeId);
		//增加文章学习数量
		if(postTypeEntity!=null){
			postTypeEntity.setBrowseNumber(postTypeEntity.getBrowseNumber()+1);
			
			System.out.println(postTypeEntity);
			postTypeService.updateBrowseNumber(postTypeEntity);
		}
		//增加用户学习数量
		
		String openId = (String) request.getSession().getAttribute("openId");
		if(openId != null){
			UserEntity userEntity = userService.findByOpenId(openId);
			if(userEntity !=null){
				userEntity.setStudyNumber(userEntity.getStudyNumber()+1);
				userService.updateStudyNumber(userEntity);
			}
		}
		
		
		System.out.println("进入文章查询：>>>"+typeId);
		model.addAttribute("postList", postService.findByPostTypeId(typeId));
		
		
		return "service/post";
	}
	
	@RequestMapping("findByPostId")
	public String findByPostId(int id,Model model){
		
		PostEntity postEntity=postService.findById(id);
		
		model.addAttribute("post",postEntity);
		
		/*if(postEntity!=null){
			//增加学习数量
			postEntity.setBrowseNumber(postEntity.getBrowseNumber()+1);
			System.out.println("postEntity:>>>"+postEntity);
			
			postService.updateBrowseNumber(postEntity);
			
			
		}*/
		
		return "service/post-content";
	}
	
	@RequestMapping("findInfo")
	public String findInfo(int postId,Model model){
		
		PostEntity postEntity=postService.findById(postId);
		model.addAttribute("post",postEntity);
		
		return "serve/postInfo";
	}
	
}
